package com.example.appusagestats

import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.appusagestats.adapter.UsageDetailAdapter
import com.example.appusagestats.manager.AppUsageStatsManager
import com.example.appusagestats.model.AppUsageInfo
import kotlinx.coroutines.*

class AppDetailActivity : AppCompatActivity() {

    private lateinit var usageStatsManager: AppUsageStatsManager
    private lateinit var usageDetailAdapter: UsageDetailAdapter
    private lateinit var appIcon: ImageView
    private lateinit var appName: TextView
    private lateinit var packageName: TextView
    private lateinit var totalUsage: TextView
    private lateinit var usageDetailsRecyclerView: RecyclerView

    private var packageNameStr: String = ""
    private var appNameStr: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_app_detail)

        packageNameStr = intent.getStringExtra("package_name") ?: ""
        appNameStr = intent.getStringExtra("app_name") ?: ""

        initViews()
        initUsageStatsManager()
        setupRecyclerView()
        setupToolbar()
        loadAppDetails()
    }

    private fun initViews() {
        appIcon = findViewById(R.id.appIcon)
        appName = findViewById(R.id.appName)
        packageName = findViewById(R.id.packageName)
        totalUsage = findViewById(R.id.totalUsage)
        usageDetailsRecyclerView = findViewById(R.id.usageDetailsRecyclerView)
    }

    private fun initUsageStatsManager() {
        usageStatsManager = AppUsageStatsManager(this)
    }

    private fun setupRecyclerView() {
        usageDetailAdapter = UsageDetailAdapter(emptyList())
        usageDetailsRecyclerView.apply {
            layoutManager = LinearLayoutManager(this@AppDetailActivity)
            adapter = usageDetailAdapter
        }
    }

    private fun setupToolbar() {
        val toolbar = findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbar)
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        toolbar.setNavigationOnClickListener {
            finish()
        }
    }

    private fun loadAppDetails() {
        CoroutineScope(Dispatchers.Main).launch {
            val appUsageInfo = withContext(Dispatchers.IO) {
                getAppUsageInfo()
            }
            
            updateUI(appUsageInfo)
        }
    }

    private fun getAppUsageInfo(): AppUsageInfo? {
        val todayStats = usageStatsManager.getTodayAppUsageStats()
        return todayStats.find { it.packageName == packageNameStr }
    }

    private fun updateUI(appUsageInfo: AppUsageInfo?) {
        if (appUsageInfo != null) {
            appName.text = appUsageInfo.appName
            packageName.text = appUsageInfo.packageName
            totalUsage.text = "总使用时长: ${appUsageInfo.getFormattedUsageTime()}"
            
            appUsageInfo.appIcon?.let { icon ->
                appIcon.setImageDrawable(icon)
            } ?: run {
                appIcon.setImageResource(R.drawable.ic_default_app)
            }

            usageDetailAdapter.updateData(appUsageInfo.usageEvents)
        } else {
            // 如果没有找到应用信息，显示基本信息
            appName.text = appNameStr
            packageName.text = packageNameStr
            totalUsage.text = "总使用时长: 0分钟"
            appIcon.setImageResource(R.drawable.ic_default_app)
            usageDetailAdapter.updateData(emptyList())
        }
    }
} 